/*
 * FriendSelector Entry processor for getting FriendIDs randomly or paged from the grid.
 * @Author: Onur AKTAS 21-03-2009 - onur.aktas@live.com
 */

package com.myfoon.model.entryprocessor;

import com.tangosol.util.InvocableMap;
import com.tangosol.util.processor.AbstractProcessor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class FriendSelector extends AbstractProcessor {
    private boolean random;
    private int count;

    private int start;
    private int max;

    private int processType;


    public FriendSelector() {
    }

    public FriendSelector(boolean random, int count) {
        this.random = random;
        this.count = count;
        this.processType = 1;
    }

    public FriendSelector(int start, int max) {
        this.start = start;
        this.max = max;
        this.processType = 2;
    }
    
    /*
     * Processor code
     */
    public Object process(InvocableMap.Entry entry) {
        if (entry == null)
            return null;

        if (entry.getValue() == null) {
            return null;
        }
        
        List<Long> friendList = (List<Long>)entry.getValue();
        
        Object[] result = new Object[2];
        
        if (friendList.size() == 0) {
            result[0] = 0;
            result[1] = friendList;
            return result;
        }                
        
        result[0] = friendList.size();
        
        if (processType == 1) {
            result[1] = getRandomFriends(friendList);
        }
        else if (processType == 2) {
            result[1] = getPagedFriends(friendList);
        }
        
        return result;
    }

    /*
     * Returns random friend IDs 
     */
    private List getRandomFriends(List<Long> friendList) {
        Random random = new Random();
        List<Long> randomList = new ArrayList<Long>();
        
        if (friendList.size() <= count) {
            return friendList;
        }
                
        
        while (randomList.size() < count) {
            int rn = random.nextInt(friendList.size());
            if (rn < 0)
                rn *= -1;

            while (!randomList.contains(friendList.get(rn))) {
                randomList.add(friendList.get(rn));
            }
        }
        
        return randomList;
    }

    /*
    Returns paged friend IDs 
     */
    private List getPagedFriends(List<Long> friendList) {
        List<Long> friendSubList = new ArrayList<Long>();
        
        if (start < friendList.size() ) {
            if ( start + max < friendList.size() ) {
                friendSubList.addAll(friendList.subList(start, start + max));                
            }
            else {
                friendSubList.addAll(friendList.subList(start, friendList.size()));                
            }
        }
        
        return friendSubList;
    }

    public void setRandom(boolean random) {
        this.random = random;
    }

    public boolean isRandom() {
        return random;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getStart() {
        return start;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMax() {
        return max;
    }

    public void setProcessType(int processType) {
        this.processType = processType;
    }

    public int getProcessType() {
        return processType;
    }
}
